//All_Student_Information
// ajax获取数据
let xhr = new XMLHttpRequest()
xhr.open("GET", "https://www.bingjs.com:8001/Student/GetAll")
xhr.send()
xhr.onreadystatechange = function () {
    if (xhr.readyState === 4 && xhr.status === 200) {
        // 获取数据
        let str = JSON.parse(xhr.response)
        console.log(str);
        getAll(str)
    }
}

// 封装函数 将数据在页面中显示
function getAll(stu_infor) {
    stu_infor.forEach(a => {
        let tr = document.createElement("tr")
        let td1 = document.createElement("td")
        // 获取内容 一定要注意后端接口中的属性名
        td1.innerHTML = a.StudentNo
        let td2 = document.createElement("td")
        td2.innerHTML = a.LoginPwd
        let td3 = document.createElement("td")
        td3.innerHTML = a.StudentName
        let td4 = document.createElement("td")
        td4.innerHTML = a.Sex === "M" ? "男" : "女"
        let td5 = document.createElement("td")
        td5.innerHTML = a.GradeId
        let td6 = document.createElement("td")
        // td6.innerHTML = a.BornDate
        td6.innerHTML = bornDate(new Date(a.BornDate))
        let td7 = document.createElement("td")
        td7.innerHTML = a.Phone
        let td8 = document.createElement("td")
        td8.innerHTML = a.Address
        let td9 = document.createElement("td")
        td9.innerHTML = a.Email
        tr.appendChild(td1)
        tr.appendChild(td2)
        tr.appendChild(td3)
        tr.appendChild(td4)
        tr.appendChild(td5)
        tr.appendChild(td6)
        tr.appendChild(td7)
        tr.appendChild(td8)
        tr.appendChild(td9)
        document.querySelector("tbody").appendChild(tr)
    });
}

//修改日期格式
function bornDate(data) {
    let year = data.getFullYear()
    let month = data.getMonth() + 1
    let day = data.getDate()
    // console.log(year, month, day);
    return [year, month, day].map(a => pacth0(a)).join("-")

}

// 补0操作 如果是单数就添加一个0
function pacth0(val) {
    return (val + "").length > 1 ? val : "0" + val
}